//
//  MSImageFullScreenManager.m
//  MSJD
//
//  Created by 箭军刘 on 2018/12/23.
//  Copyright © 2018 xiangbo. All rights reserved.
//

#import "MSImageFullScreenManager.h"
#import "SDCycleScrollView.h"

static CGRect oldframe;

@implementation MSImageFullScreenManager

+ (void)scanBigImageWithImageView:(UIImageView *)sourceImageview {
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    [MSImageFullScreenManager scanBigImageWithImage:sourceImageview.image frame:[sourceImageview convertRect:sourceImageview.bounds toView:window]];
}

+ (void)scanBigImageWithImage:(UIImage *)image frame:(CGRect)pOldframe {
    oldframe = pOldframe;
    //当前视图
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    //背景
    UIView *backgroundView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)];
    [backgroundView setBackgroundColor:[UIColor colorWithHexString:@"0x181a20"]];
    //此时视图不会显示
    [backgroundView setAlpha:0];
    //将所展示的imageView重新绘制在Window中
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:oldframe];
    [imageView setImage:image];
    [imageView setTag:1024];
    [backgroundView addSubview:imageView];
    //将原始视图添加到背景视图中
    [window addSubview:backgroundView];
    
    //添加点击事件同样是类方法 -> 作用是再次点击回到初始大小
    UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideImageView:)];
    [backgroundView addGestureRecognizer:tapGestureRecognizer];
    
    //动画放大所展示的ImageView
    
    [UIView animateWithDuration:0.4 animations:^{
        CGFloat y,width,height;
        y = ([UIScreen mainScreen].bounds.size.height - image.size.height * [UIScreen mainScreen].bounds.size.width / image.size.width) * 0.5;
        //宽度为屏幕宽度
        width = [UIScreen mainScreen].bounds.size.width;
        //高度 根据图片宽高比设置
        height = image.size.height * [UIScreen mainScreen].bounds.size.width / image.size.width;
        [imageView setFrame:CGRectMake(0, y, width, height)];
        //重要！ 将视图显示出来
        [backgroundView setAlpha:1];
    } completion:^(BOOL finished) {
        
    }];
}

/**
 *  恢复imageView原始尺寸
 *
 *  @param tap 点击事件
 */
+ (void)hideImageView:(UITapGestureRecognizer *)tap{
    UIView *backgroundView = tap.view;
    //原始imageview
    UIImageView *imageView = [tap.view viewWithTag:1024];
    //恢复
    [UIView animateWithDuration:0.4 animations:^{
        [imageView setFrame:oldframe];
        [backgroundView setAlpha:0];
    } completion:^(BOOL finished) {
        //完成后操作->将背景视图删掉
        [backgroundView removeFromSuperview];
    }];
}

+ (void)scanBigImageListWithImageUrlList:(NSArray *)imageUrlList sourceImageView:(nonnull UIImageView *)sourceImage selectIndex:(NSInteger)selectIndex {
    //当前视图
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    //背景
    NSMutableArray *titleArr = [NSMutableArray array];
    for (NSInteger i = 0; i < imageUrlList.count; i ++) {
        [titleArr addObject:[NSString stringWithFormat:@"%ld/%ld", i + 1, imageUrlList.count]];
    }
    SDCycleScrollView *cycleScrollView = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(0, 0, MS_SCREEN_WIDTH, MS_SCREEN_HEIGHT) imageURLStringsGroup:imageUrlList];
    cycleScrollView.autoScroll = NO;
    cycleScrollView.infiniteLoop = NO;
    cycleScrollView.bannerImageViewContentMode = UIViewContentModeScaleAspectFit;
    cycleScrollView.pageDotColor = [UIColor grayColor];
    cycleScrollView.backgroundColor = [UIColor colorWithHexString:@"#4a4a4a"];
    cycleScrollView.tag = 2048;
    __weak typeof(SDCycleScrollView *) weakScrollView = cycleScrollView;
    cycleScrollView.clickItemOperationBlock = ^(NSInteger currentIndex) {
        [MSImageFullScreenManager closeCycleScrollViewWithSDCycleScrollView:weakScrollView];
    };
    [window addSubview:cycleScrollView];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.01 * NSEC_PER_SEC)),dispatch_get_main_queue(), ^{
        [MSImageFullScreenManager changeToWithSDCycleScrollView:weakScrollView targetIcon:selectIndex];
    });
}

+ (void)changeToWithSDCycleScrollView:(SDCycleScrollView *)scrollView targetIcon:(NSInteger)selectIndex {
    [scrollView makeScrollViewScrollToIndex:selectIndex];
}

+ (void)closeCycleScrollViewWithSDCycleScrollView:(SDCycleScrollView *)scrollView {
    [UIView animateWithDuration:0.4 animations:^{
//        [self.imageScroll setFrame:oldframe];
        scrollView.alpha = 0.0;
    } completion:^(BOOL finished) {
        //完成后操作->将背景视图删掉
        [scrollView removeFromSuperview];
    }];
}

@end
